﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using YJKRoom.NumberRoomControler;
using YJKRoom.Utility;

namespace YJKRoom.NumberRoomSetting
{
	public partial class DlgRoomNumberSettings : System.Windows.Forms.Form
	{
		public DlgRoomNumberSettings()
		{
			this.InitializeComponent();
		}

		public DlgRoomNumberSettings(ExternalCommandData cmdData, bool bEngRoomName = false)
		{
			this.InitializeComponent();
			this.m_Revit = cmdData;
			this.m_bEngRoomName = bEngRoomName;
			string xmlFilePath = Product.UserDataLocation + "\\RoomNumberSettings.xml";
			string text = "";
			this.ReadDataFromXML(xmlFilePath, ref text);
			this.SetControlVisiable();
		}

		public bool ReadDataFromXML(string xmlFilePath, ref string errMessage)
		{
			if (!File.Exists(xmlFilePath))
			{
				return false;
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(xmlFilePath);
			XmlNode xmlNode = xmlDocument.SelectSingleNode(this.RootNodeName);
			if (xmlNode == null)
			{
				errMessage = "文件格式不符！";
				return false;
			}
			XmlNode xmlNode2 = xmlNode.SelectSingleNode(this.DataNodeName);
			if (xmlNode2 == null)
			{
				errMessage = "文件中缺少数据！";
				return false;
			}
			return this.DoReadInfo(0, xmlNode2, ref errMessage);
		}

		private bool DoReadInfo(int type, XmlNode xnNode, ref string errMessage)
		{
			try
			{
				XmlNode xmlNode = xnNode.SelectSingleNode("房间名称");
				bool @checked = Convert.ToBoolean(xmlNode.InnerText);
				this.cbName.Checked = @checked;
			}
			catch
			{
				this.cbName.Checked = false;
			}
			try
			{
				XmlNode xmlNode = xnNode.SelectSingleNode("单元");
				bool @checked = Convert.ToBoolean(xmlNode.InnerText);
				this.cbUnit.Checked = @checked;
			}
			catch
			{
				this.cbUnit.Checked = false;
			}
			try
			{
				XmlNode xmlNode = xnNode.SelectSingleNode("房间编号");
				bool @checked = Convert.ToBoolean(xmlNode.InnerText);
				this.cbNumber.Checked = @checked;
			}
			catch
			{
				this.cbNumber.Checked = false;
			}
			try
			{
				XmlNode xmlNode = xnNode.SelectSingleNode("前缀");
				this.tbPrefix.Text = xmlNode.InnerText;
			}
			catch
			{
				this.tbPrefix.Text = "";
			}
			try
			{
				XmlNode xmlNode = xnNode.SelectSingleNode("后缀");
				this.tbSuffix.Text = xmlNode.InnerText;
			}
			catch
			{
				this.tbSuffix.Text = "";
			}
			try
			{
				XmlNode xmlNode = xnNode.SelectSingleNode("房间面积");
				bool @checked = Convert.ToBoolean(xmlNode.InnerText);
				this.cbArea.Checked = @checked;
			}
			catch
			{
				this.cbArea.Checked = false;
			}
			try
			{
				XmlNode xmlNode = xnNode.SelectSingleNode("方式1");
				bool @checked = Convert.ToBoolean(xmlNode.InnerText);
				this.rbText.Checked = @checked;
			}
			catch
			{
				this.rbText.Checked = false;
			}
			try
			{
				XmlNode xmlNode = xnNode.SelectSingleNode("方式2");
				bool @checked = Convert.ToBoolean(xmlNode.InnerText);
				this.rbSymbol.Checked = @checked;
				if (!this.rbText.Checked && !this.rbSymbol.Checked)
				{
					this.rbSymbol.Checked = true;
				}
			}
			catch
			{
				this.rbSymbol.Checked = false;
			}
			try
			{
				XmlNode xmlNode = xnNode.SelectSingleNode("自动更新");
				bool @checked = Convert.ToBoolean(xmlNode.InnerText);
				this.cbChangeExist.Checked = @checked;
			}
			catch
			{
				this.cbChangeExist.Checked = false;
			}
			return true;
		}

		private void WriteModuleXMLFile(string fileName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			bool bExistXml;
			if (File.Exists(fileName))
			{
				xmlDocument.Load(fileName);
				bExistXml = true;
			}
			else
			{
				XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
				xmlDocument.AppendChild(newChild);
				XmlElement newChild2 = xmlDocument.CreateElement(this.RootNodeName);
				xmlDocument.AppendChild(newChild2);
				bExistXml = false;
			}
			this.ExportInfos(this.DataNodeName, xmlDocument, bExistXml);
			xmlDocument.Save(fileName);
		}

		private void ExportInfos(string dataNodeName, XmlDocument xmlDoc, bool bExistXml)
		{
			XmlNode xmlNode = xmlDoc.SelectSingleNode(this.RootNodeName);
			if (bExistXml)
			{
				XmlNode xmlNode2 = xmlNode.SelectSingleNode(dataNodeName);
				if (xmlNode2 != null)
				{
					xmlNode2.RemoveAll();
					xmlNode.RemoveChild(xmlNode2);
				}
			}
			XmlElement newChild = xmlDoc.CreateElement(dataNodeName);
			xmlNode.AppendChild(newChild);
			XmlNode xmlNode3 = xmlNode.SelectSingleNode(dataNodeName);
			XmlElement xmlElement = xmlDoc.CreateElement("房间名称");
			xmlElement.InnerText = this.cbName.Checked.ToString();
			xmlNode3.AppendChild(xmlElement);
			xmlElement = xmlDoc.CreateElement("单元");
			xmlElement.InnerText = this.cbUnit.Checked.ToString();
			xmlNode3.AppendChild(xmlElement);
			xmlElement = xmlDoc.CreateElement("房间编号");
			xmlElement.InnerText = this.cbNumber.Checked.ToString();
			xmlNode3.AppendChild(xmlElement);
			xmlElement = xmlDoc.CreateElement("前缀");
			xmlElement.InnerText = this.tbPrefix.Text;
			xmlNode3.AppendChild(xmlElement);
			xmlElement = xmlDoc.CreateElement("后缀");
			xmlElement.InnerText = this.tbSuffix.Text;
			xmlNode3.AppendChild(xmlElement);
			xmlElement = xmlDoc.CreateElement("房间面积");
			xmlElement.InnerText = this.cbArea.Checked.ToString();
			xmlNode3.AppendChild(xmlElement);
			xmlElement = xmlDoc.CreateElement("方式1");
			xmlElement.InnerText = this.rbText.Checked.ToString();
			xmlNode3.AppendChild(xmlElement);
			xmlElement = xmlDoc.CreateElement("方式2");
			xmlElement.InnerText = this.rbSymbol.Checked.ToString();
			xmlNode3.AppendChild(xmlElement);
			xmlElement = xmlDoc.CreateElement("自动更新");
			xmlElement.InnerText = this.cbChangeExist.Checked.ToString();
			xmlNode3.AppendChild(xmlElement);
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			string fileName = Product.UserDataLocation + "\\RoomNumberSettings.xml";
			this.WriteModuleXMLFile(fileName);
			if (this.cbChangeExist.Checked)
			{
				try
				{
					Document document = this.m_Revit.Application.ActiveUIDocument.Document;
                    Autodesk.Revit.DB.View activeView = this.m_Revit.Application.ActiveUIDocument.ActiveView;
					IList<Element> elementByCategory = YJKElementFilter.GetElementByCategory(document, BuiltInCategory.OST_RoomTags);
					foreach (Element element in elementByCategory)
					{
						RoomTag roomTag = element as RoomTag;
						if (roomTag != null)
						{
							RoomTagType roomTagType = RoomNumberDataOperator.GetRoomTagType(this.m_Revit, this.m_bEngRoomName, true);
							if (roomTagType != null)
							{
								roomTag.RoomTagType = roomTagType;
								if (roomTag.Room != null && this.cbNumber.Checked)
								{
									ElementParameter.SetParameterValueEx(roomTag.Room, "前缀", this.tbPrefix.Text, (Autodesk.Revit.DB.ParameterType)1);
									ElementParameter.SetParameterValueEx(roomTag.Room, "后缀", this.tbSuffix.Text, (Autodesk.Revit.DB.ParameterType)1);
								}
								if (roomTag.Room != null && this.cbArea.Checked)
								{
									string value = this.rbSymbol.Checked ? "㎡" : "平方米";
									ElementParameter.SetParameterValueEx(roomTag.Room, "房间编号面积单位", value, (Autodesk.Revit.DB.ParameterType)1);
								}
							}
						}
					}
					Common.UpdateModel(this.m_Revit.Application.ActiveUIDocument, false);
				}
				catch (Exception)
				{
				}
			}
		}

		protected bool SetParametersValue(Element element, string sPrefix, string sSurfix)
		{
			if (element == null)
			{
				return false;
			}
			bool result;
			try
			{
				ElementParameter.SetParameterValueEx(element, "前缀", sPrefix, (Autodesk.Revit.DB.ParameterType)1);
				ElementParameter.SetParameterValueEx(element, "后缀", sSurfix, (Autodesk.Revit.DB.ParameterType)1);
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private void cbArea_CheckedChanged(object sender, EventArgs e)
		{
			this.SetControlVisiable();
		}

		private void SetControlVisiable()
		{
			this.rbText.Enabled = this.cbArea.Checked;
			this.rbSymbol.Enabled = this.cbArea.Checked;
			this.tbPrefix.Enabled = this.cbNumber.Checked;
			this.tbSuffix.Enabled = this.cbNumber.Checked;
		}

		private void cbNumber_CheckedChanged(object sender, EventArgs e)
		{
			this.SetControlVisiable();
		}

		private ExternalCommandData m_Revit;

		private string RootNodeName = "RoomNumberSettingsData";

		private string DataNodeName = "Data";

		private bool m_bEngRoomName;
	}
}
